/*Used for new hires (j=2) and stayers (j=4)
James August 7, 2024 */

*Nov 17 version
*Get rid of missing wages (self-employed) 
*Update all the way to 2016 
*5 segments instead of 10
*Quadratic provincial trends
*Prov*bin and year*bin as in triple diff
*full prov*dummies in lower segment
*Add dummy for integer dollar value
*allow for different effects just after increase



/*******************************
This program is the same as James' code 
except here I use 3 month dummy variable.

That is, dummy for upcoming minimum wage change.


*******************************/










* This version of the code estimates full models for men and women with and without
* the "B" restrictions.log using $logdir\Townsend_hzd_sep_5_2014,text replace

clear
discard
cap log close

set more off
set matsize 800
*set mem 1200m

/* "pub" is a flag for testing the code using the public use files and should be always set to zero when using the data in the RDC. */
local pub=0



* workdata is the directory that contains the working data sets
global workdatadir "P:\Townsend_5609_wages\November2019\WorkingData"
global logdir "P:\Townsend_5609_wages\March_2024\logfiles"
global resultdir "P:\Townsend_5609_wages\March_2024\results"

*global workdatadir "P:\Townsend_3234\data"
*global logdir "P:\Townsend_3234\STATA_code\log_files"
*global resultdir "P:\Townsend_3234\results"

log using $logdir\hzd_estimator_sep_10_2024_leavers_layoff_1_year_forward.log, append



/****************************************************
* This is the old data version of min wage changes


/******TL: CREATE DATA SET KEEPING TRACK OF MW CHANGES******/
u $workdatadir\min_wag_1996_2017
sort prov year month

by prov: gen mw=mwage[_n-3]
gen chg=((mwage-mw)/mw)
gen mwchg=chg>0 /*could play with this*/




by prov: gen l5mw=mwage[_n-12]
gen chg=((mwage-l5mw)/l5mw)
gen mwchg=chg>0 /*could play with this*/



keep if year>=1997 & year<=2016
/*****JT: Moved to monthly************/
keep prov year month mwchg
sort prov year month
save $workdatadir\mwchg, replace
clear
/*END OF MW CHG LOOP*/

****************************************************/


/*****************************************************
Create dummy for upcoming min wage change for leavers
****************************************************/

u $workdatadir\min_wag_1996_2017 ,clear

* Get dummy for min wage change coming in next 3 months

bys prov (year month): g time=_n
tsset prov time,delta(1)

g change=mwage-L.mwage
replace change=1 if change!=0 & change!=.
g temp=change

g mwchg=1 if temp==1 
replace mwchg=1 if L.temp==1 
replace mwchg=0 if mwchg!=1
replace mwchg=. if time==1
drop temp

keep if year>=1997 & year<=2016
keep prov year month mwchg
save $workdatadir\mwchg_current.dta ,replace


/*James JULY 28, 2024 - New code added to track time since minimum wage****
sincechange is a counter - number of periods since change in minimum wage, 
where value is zero in first month that minimum wage changes from previous month's.
Also note that currently we only go back to January 1996, and so in January, the previous period is missing, so the count starts at 0 in this month. Not relavent, since by january 1997, the only relevant dynamics at the moment are relative to 
1997 minimum wage changes (there were no minimum wage changes in December 1995). 

*Written for joiners...would need to be changed for forward looking estimator.
*/

u $workdatadir\min_wag_1996_2017 ,clear

bys prov (year month): g time=_n
tsset prov time,delta(1)
by prov (time): g nochange=mwage==l.mwage /* nochange =1 if same minimum wage as last month*/

gen sincechange = 0 /*counter, months since last minimum wage change */

foreach period of numlist 1/260 {
replace sincechange=l1.sincechange+1 if time==`period' & nochange==1 
replace sincechange=0 if time==`period' & nochange==0	
}
table time if prov==10,stat(mean mwage) stat(mean nochange) stat(mean sincechange)
gen mwage2=l2.mwage
gen mwage312=.
foreach month of numlist 2/11 {
	local j=`month'+1
replace mwage312=l`j'.mwage if sincechange==`month' 
}
table time if prov==10,stat(mean mwage) stat(mean nochange) stat(mean sincechange) stat(mean mwage3) stat(mean mwage312)

gen mwchg12=sincechange==0|sincechange==1
gen mwchg312=sincechange>=2&sincechange<=11

replace mwage2=0 if mwage2==.
replace mwage312=0 if mwage312==.

keep prov year month cpi mwage sincechange mwchg12 mwchg312 mwage2 mwage312

merge 1:1 prov year month using $workdatadir\mwchg_current
drop _merge
list if prov==10 & year<=1998

drop mwchg
keep if year>=1997 & year<=2016

save $workdatadir\min_wage_312.dta ,replace

/***** Until next change**************/
 
u $workdatadir\min_wag_1996_2017 ,clear

bys prov (year month): g time=_n
tsset prov time,delta(1)
by prov (time): g nochangef=mwage==l.mwage /*minimum wage same as last month*/

gen untilchange = 0 /*counter, months since last minimum wage change */
gen newtime=261-time


foreach period of numlist 1/260 {
replace untilchange=f1.untilchange+1 if newtime==`period' & nochange==1 
replace untilchange=0 if newtime==`period' & nochange==0	
}
*table time year month if prov==10,stat(mean mwage) stat(mean nochange) stat(mean untilchange)
gen mwagef2=f2.mwage


gen mwchgf12=untilchange==1|untilchange==2

replace mwagef2=0 if mwagef2==.

keep prov year month cpi mwage mwagef2 untilchange mwchgf12 mwagef2

merge 1:1 prov year month using $workdatadir\mwchg_current
drop _merge
list if prov==10 & year<=1998

drop mwchg
keep if year>=1997 & year<=2016

save $workdatadir\min_wage_12f.dta ,replace

/*****************************************************
Get new minimum wage in third month
****************************************************/

use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage3=mwage
g cpi3=cpi

keep if year>=1997 & year<=2016
keep prov year month mwage3 cpi3

save $workdatadir\min_wag_1996_2017_month3.dta,replace


use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage3=L2.mwage
g cpi3=L2.cpi

keep if year>=1997 & year<=2016
keep prov year month mwage3 cpi3
ren cpi3 cpi
ren mwage3 mwage

save $workdatadir\min_wag_1996_2017_month1.dta,replace

use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage12=L12.mwage
g cpi12=L12.cpi

keep if year>=1997 & year<=2016
keep prov year month mwage12


save $workdatadir\min_wag_1996_2017_month12.dta,replace




/*Estimation loops begin here. */

local sex=1


while `sex'<=3 {

	* `k' is the sample counter for broad criteria (see below)

	local k=1
	/*k indicates the data set to be created
	Currently:  1 = full data set
				2 = only those paid by the hour.
				3 = excluding those earning tips. */






	* `j' is the subsample counter (see below)



foreach j of numlist 1/2 {

		* Loads base data set - additional restrictions to follow.
		if `pub'~=0 use $workdatadir\est_dat,clear
		if `pub'~=1 use $workdatadir\panel_6M_sample,clear

		
		/* Drop observations with partial imputation, etc. */

		drop if flag_restrictionsB==1
		*Testing restrictions

		display `data after flag_restrictionsB applied'
		sum

		/* Gender selection */
		if `sex'==1 {
			keep if female==0
		}

		if `sex'==2 {
			keep if female==1
		}

		/*Additional possible deletion criteria */

		if `k'==2 {
			/*Normally paid by hour*/
			keep if hourlywork1==1
		}

		if `k'==3 {
			/*No tips */
			keep if tips1~=1
		}

		/*Subsample criteria based on `j' counter */

	* `j' is the subsample counter (see below)


		* Loads base data set - additional restrictions to follow.
		use $workdatadir\panel_6M_sample,clear

		
		
		/* Drop observations with partial imputation, etc. */

		drop if flag_restrictionsB==1
		*Testing restrictions

		display `data after flag_restrictionsB applied'
		sum

		/* Gender selection */
		if `sex'==1 {
			keep if female==0
		}

		if `sex'==2 {
			keep if female==1
		}

		/*Additional possible deletion criteria */

		if `k'==2 {
			/*Normally paid by hour*/
			keep if hourlywork1==1
		}

		if `k'==3 {
			/*No tips */
			keep if tips1~=1
		}

		/*Subsample criteria based on `j' counter */

		if `j'==1 {
			/* Leavers*/
			
			* This removes all lfsstat3==. 
			drop if lfsstat3==. & tenure3==.
			drop if lfsstat2==. & tenure2==. 
			
			* This works fine now.
			keep if lfsstat3>2|tenure3<=2
			* We have 150k people here
			
			
			* Here we assume this is a quit or layoff from period 1 job
			g quitter=1 if inlist(whyleftv2_2,0,1,2,3,5,6,7)
			g layoff=1 if inlist(whyleftv2_2,4)
			
			* Drop people who aren't working but don't answer the question
			drop if lfsstat2>2 & whyleftv2_2==.
			
			replace layoff=0 if layoff==.
			replace quitter=0 if quitter==.
			g unemployed2=quitter+layoff
			tab unemployed2 lfsstat2
			
			
			* Identify clear instances where individuals stayed in the same job
			g stillworking=1 if lfsstat2<=2 & tenure2>tenure1 | lfsstat2<=2 & tenure2>2 & tenure2!=.
			* Leaves around 13k people who may have started a new job or continued working in the same job.
			* But we can't be clear which it is and so can't accurately identify layoffs/quits/job2job
			* Drop these guys
			drop if stillworking==. & unemployed2==0
			
			
			* For the guys still working in the same job identify quitters and layoffs
			g quitter2=1 if stillworking==1 & inlist(whyleftv2_3,0,1,2,3,5,6,7)
			g layoff2=1 if stillworking==1 & inlist(whyleftv2_3,4)
			replace layoff2=0 if layoff2==.
			replace quitter2=0 if quitter2==.
			
			g unemployed3=quitter2+layoff2
			
			* There are some people that do not answer this question and are still working.
			* These are guys we assume who are working new jobs
			* I can see that their tenure is 1-2 months long which somewhat confirms this.
			drop if stillworking==1 & unemployed3==0
			
		
			replace quitter=quitter+quitter2
			replace layoff=layoff+layoff2
			* Left with 100k people
			
			keep if layoff==1
			* Sample of 37K
		}
		
		if `j'==2 {
			/* Leavers - tenure1<=12*/
			
			keep if tenure1<=12
			
			* This removes all lfsstat3==. 
			drop if lfsstat3==. & tenure3==.
			drop if lfsstat2==. & tenure2==. 
			
			* This works fine now.
			keep if lfsstat3>2|tenure3<=2
			* We have 150k people here
			
			
			* Here we assume this is a quit or layoff from period 1 job
			g quitter=1 if inlist(whyleftv2_2,0,1,2,3,5,6,7)
			g layoff=1 if inlist(whyleftv2_2,4)
			
			* Drop people who aren't working but don't answer the question
			drop if lfsstat2>2 & whyleftv2_2==.
			
			replace layoff=0 if layoff==.
			replace quitter=0 if quitter==.
			g unemployed2=quitter+layoff
			tab unemployed2 lfsstat2
			
			
			* Identify clear instances where individuals stayed in the same job
			g stillworking=1 if lfsstat2<=2 & tenure2>tenure1 | lfsstat2<=2 & tenure2>2 & tenure2!=.
			* Leaves around 13k people who may have started a new job or continued working in the same job.
			* But we can't be clear which it is and so can't accurately identify layoffs/quits/job2job
			* Drop these guys
			drop if stillworking==. & unemployed2==0
			
			
			* For the guys still working in the same job identify quitters and layoffs
			g quitter2=1 if stillworking==1 & inlist(whyleftv2_3,0,1,2,3,5,6,7)
			g layoff2=1 if stillworking==1 & inlist(whyleftv2_3,4)
			replace layoff2=0 if layoff2==.
			replace quitter2=0 if quitter2==.
			
			g unemployed3=quitter2+layoff2
			
			* There are some people that do not answer this question and are still working.
			* These are guys we assume who are working new jobs
			* I can see that their tenure is 1-2 months long which somewhat confirms this.
			drop if stillworking==1 & unemployed3==0
			
		
			replace quitter=quitter+quitter2
			replace layoff=layoff+layoff2
			* Left with 100k people
			
			keep if layoff==1
			* Sample of 37K
		}

		if `j'==3 {
			/* Leavers - tenure1>=13*/
			keep if tenure1>=13
			* This removes all lfsstat3==. 
			drop if lfsstat3==. & tenure3==.
			drop if lfsstat2==. & tenure2==. 
			
			* This works fine now.
			keep if lfsstat3>2|tenure3<=2
			* We have 150k people here
			
			
			* Here we assume this is a quit or layoff from period 1 job
			g quitter=1 if inlist(whyleftv2_2,0,1,2,3,5,6,7)
			g layoff=1 if inlist(whyleftv2_2,4)
			
			* Drop people who aren't working but don't answer the question
			drop if lfsstat2>2 & whyleftv2_2==.
			
			replace layoff=0 if layoff==.
			replace quitter=0 if quitter==.
			g unemployed2=quitter+layoff
			tab unemployed2 lfsstat2
			
			
			* Identify clear instances where individuals stayed in the same job
			g stillworking=1 if lfsstat2<=2 & tenure2>tenure1 | lfsstat2<=2 & tenure2>2 & tenure2!=.
			* Leaves around 13k people who may have started a new job or continued working in the same job.
			* But we can't be clear which it is and so can't accurately identify layoffs/quits/job2job
			* Drop these guys
			drop if stillworking==. & unemployed2==0
			
			
			* For the guys still working in the same job identify quitters and layoffs
			g quitter2=1 if stillworking==1 & inlist(whyleftv2_3,0,1,2,3,5,6,7)
			g layoff2=1 if stillworking==1 & inlist(whyleftv2_3,4)
			replace layoff2=0 if layoff2==.
			replace quitter2=0 if quitter2==.
			
			g unemployed3=quitter2+layoff2
			
			* There are some people that do not answer this question and are still working.
			* These are guys we assume who are working new jobs
			* I can see that their tenure is 1-2 months long which somewhat confirms this.
			drop if stillworking==1 & unemployed3==0
			
		
			replace quitter=quitter+quitter2
			replace layoff=layoff+layoff2
			* Left with 100k people
			
			keep if layoff==1
			* Sample of 37K
		}

		if `j'==4 {
			/* Continuers */
			keep if (lfsstat1 <= 2 & tenure1 >= 3)
		}

		if `j'==5 {
			/*New hires (up to 12 months of job tenure in first month of mini-panel*/
			keep if tenure1<=12
		}

		if `j'==6 {
			/*Old hires (more than 12 months of job tenure in first month of mini-panel*/
			keep if tenure1>12
		}
		display `sex' `j' `k'

		display `data after additional restrictions applied'
		sum

		*sum female lfsstat1 lfsstat2 hourlywork1 tips1




		*sum age female educlev prov wage1 tenure1 [aw=finalwt1]

		*Sample restrictions beyond basicsdrop if flag_restrictionsB==1

		rename smth1 month
		rename syear1 year

		sort prov year month


		/*merge prov year month using P:\Townsend_3234\data\min_wag_2013*/
		/*****TL: NEW MIN WAGE DATA SET FOR 2016*****/
		/*merge m:1 prov year month using $workdatadir\min_wag_1996_2017_month1
		drop if _merge==2
		drop _merge

		merge m:1 prov year month using $workdatadir\min_wag_1996_2017_month3
		drop if _merge==2
		drop _merge */
		
		
		merge m:1 prov year month using $workdatadir\min_wage_312
		drop if _merge==2
		drop _merge

		gen rdvwag=wage1/cpi*100
/*		/*****TL: DEFLATE THE MINIMUM WAGE, WAS ALREADY DONE FOR 1997-2011*/	
		gen rminw=mwage/cpi*100  
		gen rminw3=mwage3/cpi3*100  */

if `pub'==0 {	

		gen agecat=.
		replace agecat=1 if age>=15&age<=19
		replace agecat=2 if age>=20&age<=24
		replace agecat=3 if age>=25&age<=34
		replace agecat=4 if age>=35&age<=54
		replace agecat=5 if age>=55&age<=60

		drop if agecat==.



		label define AGECAT 1 "15-19" 2 "20-24" 3 "25-34" 4 "35-54" 5 "55-60"
		label value agecat AGECAT

	
		
		gen edgrp=.
		replace edgrp=1 if educlev>=0&educlev<=2
		replace edgrp=2 if educlev==3|educlev==4
		replace edgrp=3 if educlev>=5&educlev<=7
		replace edgrp=4 if educlev==8|educlev==9

		drop if edgrp==.

		table edgrp educlev

		label define EDGRP 1 "Less than H.S." 2 "H.S. graduate and/or some P.S." 3 "P.S. Diploma or Cert." 4 "B.A. or more"
		label value edgrp EDGRP

}

		if `pub'==1 {
		rename fweight finalwt1	
		rename agegrp agecat
}
		


		************************************************************
		*Code for lagged minimum wage

		*preserve

		*collapse mwage rminw,by(prov year)
		*gen mwage1=mwage[_n-1]
		*gen rminw1=rminw[_n-1]
		 

		*save d:\min_wag\data\min_one_year,replace
		*restore
		*rename _merge old_merge
			
		*merge m:1 prov year using d:\min_wag\data\min_one_year
		************************************************************

		gen unemp=1-(lfsstat1<=2)
		*keep if unemp==0
		sum unemp
		*gen ftstudent=schooln==2|schooln==4|schooln==6|schooln==8

		*drop if ftstudent==1

		*keep if age_12<=3

		*keep if educ90<=2
		*keep if cowmain<=2

		*keep if educlev<=3


		set more off

		gen wagcat=0
		replace wagcat=1 if rdvwag<=3
		replace wagcat=2 if rdvwag>3 & rdvwag<=3.5
		replace wagcat=3 if rdvwag>3.5 & rdvwag<=4



		/****TL: EXPAND LOOP TO FIND IF INTEGER VALUE IN THE WAGE CATEGORY*/

		local i=1
		while `i'<=160{
			replace wagcat=3+`i' if rdvwag>4+(`i'-1)*.1 & rdvwag<=4+`i'*.1
			local i=`i'+1
		}

		replace wagcat=164 if rdvwag>20
		*replace wagcat=145 if unemp==1
		*gen delta=(wagcat==145)
		*min wage & lages assigned here

		/***************JT: Fill in data set for identifying integer. */
		preserve
		sort prov year month wagcat
		collapse cpi,by(prov year month wagcat)

		fillin prov year month wagcat
		sum _fillin

		egen cpimax=max(cpi),by(prov year month)
		replace cpi=cpimax if cpi==.
		gen cut_l=0
		gen cut_h=0
		replace cut_l=0 if wagcat==1
		replace cut_l=3*cpi/100 if wagcat==2
		replace cut_l=3.5*cpi/100 if wagcat==3
		replace cut_h=3*cpi/100 if wagcat==1
		replace cut_h=3.5*cpi/100 if wagcat==2
		replace cut_h=4*cpi/100 if wagcat==3
		local i=1
		while `i'<=160{
			replace cut_l=(4+(`i'-1)*.1)*cpi/100 if wagcat==`i'+3	
			replace cut_h=(4+`i'*.1)*cpi/100 if wagcat==`i'+3
			local i=`i'+1
		}
		drop if cpi==.

		sum

		/****TL: INTEGER VALUE IN THE WAGE CATEGORY*/
		gen dollar=(floor(cut_h)>cut_l)
		summ dollar cut_l cut_h

		/*Could include province in list if cpi was done at the provincial level...*/
		keep if prov==35
		keep year month wagcat dollar
		sum
		sort year month wagcat
		table year month
		save $workdatadir\dollar_bins,replace

		restore





	*	table wagcat,c(min rdvwag max rdvwag)
		 

/* Commented out...these variables are now produced after the data is converted in wage bins 
		gen mincat=0
		replace mincat=1 if rminw<=3
		replace mincat=2 if rminw>3 & rminw<=3.5
		replace mincat=3 if rminw>3.5 & rminw<=4

		local i=1
		while `i'<=76{
			replace mincat=3+`i' if rminw>4+(`i'-1)*.1 & rminw<=4+`i'*.1
			local i=`i'+1
		}

		


		


		gen mincatn=0
		replace mincatn=1 if rminw3<=3
		replace mincatn=2 if rminw3>3 & rminw3<=3.5
		replace mincatn=3 if rminw3>3.5 & rminw3<=4

		local i=1
		while `i'<=76{
			replace mincatn=3+`i' if rminw3>4+(`i'-1)*.1 & rminw3<=4+`i'*.1
			local i=`i'+1
		}



		*table prov year,c(mean plus mean minus)

End of commenting out - James */		
 
		gen one=1


		 
		rename finalwt1 fweight 
		rename agecat agegrp 

		compress 
		 
		sort prov year month female agegrp edgrp wagcat 
		collapse (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat) 

		fillin prov year month female agegrp edgrp wagcat 

		*Checks for obs where entire spell is created by fillin command.
		egen filled=sum(_fillin),by(prov year month female agegrp edgrp)
		sum filled
		drop if filled==164
		sum filled

/*	
		
		egen minmax=max(mincat),by(prov year month)
		replace mincat=minmax if mincat==.
		drop minmax

		egen minmax=max(mincatn),by(prov year month)
		replace mincatn=minmax if mincatn==.
*/

		sort year month wagcat
		merge m:1 year month wagcat using $workdatadir\dollar_bins
		drop _merge

		sort year month wagcat
		merge m:1 prov year month using $workdatadir\min_wage_312
		drop _merge
		
		
		sort year month wagcat
		merge m:1 prov year month using $workdatadir\min_wage_312
		drop _merge

/***************Adds forward data - James 9/9/2024 ********************/
		merge m:1 prov year month using $workdatadir\min_wage_12f.dta,nogen

		*egen minmax1=max(mincat1),by(prov year month)
		*replace mincat1=minmax1 if mincat1==.

		*Fill in bins with no casualties*
		replace fweight=0 if fweight==.



		

/*	commented out...not using this. _JT	gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))
*/


		sum dollar
		summ dollar [w=fweight]

		/*****TL: MERGE TO MW CHG TO CREATE NEW DUMMIES*****/

		/*****IS: NEED TO CHANGE FILE HERE AS REQUIRED*****/
		sort prov year month
		merge m:1 prov year month using $workdatadir\mwchg_current
		drop _merge
		
		//  CONFIRM THIS
		// Don't want duplicate observations for cases where there is no min wage change
		*replace mincatn=0 if mwchg==0	



		gen quarter=.
			replace quarter=1 if month>=1&month<=3
			replace quarter=2 if month>=4&month<=6
			replace quarter=3 if month>=7&month<=9
			replace quarter=4 if month>=10&month<=12	
			
		*Added November 21, 2023 - province-quarter interaction for sole purpose of experimental clustering.
		gen prov_quarter=prov*100+quarter
			
		*table month quarter
		label define QUARTER 1 "Jan.-Mar." 2 "Apr.-Jun." 3 "Jul.-Sep." 4 "Oct.-Dec."
		label value quarter QUARTER	
		
	

		
		/********JT: removed sub as a criterion....not used below*/
/* Redundent summation & compression? 

		sort prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg

		collapse (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg) 

		compress
 End comment out*/		

 gen rminw=mwage/cpi*100
 gen rminw2=mwage2/cpi*100
 gen rminw312=mwage312/cpi*100
 gen rminwf12=mwagef2/cpi*100
 
 
 gen mincat=0
		replace mincat=1 if rminw<=3
		replace mincat=2 if rminw>3 & rminw<=3.5
		replace mincat=3 if rminw>3.5 & rminw<=4

		local i=1
		while `i'<=76{
			replace mincat=3+`i' if rminw>4+(`i'-1)*.1 & rminw<=4+`i'*.1
			local i=`i'+1
		}

		
gen mincat2=0
		replace mincat2=1 if rminw2<=3
		replace mincat2=2 if rminw2>3 & rminw2<=3.5
		replace mincat2=3 if rminw2>3.5 & rminw2<=4

		local i=1
		while `i'<=76{
			replace mincat2=3+`i' if rminw2>4+(`i'-1)*.1 & rminw2<=4+`i'*.1
			local i=`i'+1
		}
		
gen mincat312=0
		replace mincat312=1 if rminw312<=3
		replace mincat312=2 if rminw312>3 & rminw312<=3.5
		replace mincat312=3 if rminw312>3.5 & rminw312<=4

		local i=1
		while `i'<=76{
			replace mincat312=3+`i' if rminw312>4+(`i'-1)*.1 & rminw312<=4+`i'*.1
			local i=`i'+1
		}		

gen mincatf12=0
		replace mincatf12=1 if rminwf12<=3
		replace mincatf12=2 if rminwf12>3 & rminwf12<=3.5
		replace mincatf12=3 if rminwf12>3.5 & rminwf12<=4

		local i=1
		while `i'<=76{
			replace mincatf12=3+`i' if rminwf12>4+(`i'-1)*.1 & rminwf12<=4+`i'*.1
			local i=`i'+1
		} 
 
		/*Insert estimation code here */

		set more off


		*Fillin command creates observations for early 1996 as well, which have no obs. from the LFS.
		*also observations in 2012 (December) that are empty.
		drop if mincat==. & year==1996
		drop if mincat==. & year==2016
		drop if year==1996 /*currently missing 1995 minimum wages.*/
		*Drop if group has failed in earlier bins.

		
		*Selection criteria


		*gen month=11
		*gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))
		*drop month

		gen diff=wagcat-mincat
		gen diff2=wagcat-mincat2
		gen diff312=wagcat-mincat312
		gen difff12=wagcat-mincatf12


		gen min6b1=diff<-5
		gen min35b1=diff>=-5&diff<=-3
		gen min12b1=diff==-2|diff==-1
		gen min1=diff==0
		gen min12a1=diff>=1&diff<=2
		gen min35a1=diff>=3&diff<=5
		gen min610a1=diff>=6&diff<=10
		gen min1115a1=diff>=11&diff<=15
		gen min1620a1=diff>=16&diff<=20
		gen min2125a1=diff>=21&diff<=25
		gen min2630a1=diff>=26&diff<=30	
		gen min3135a1=diff>=31&diff<=35
		gen min3640a1=diff>=36&diff<=40	
		gen min4145a1=diff>=41&diff<=45
		gen min4650a1=diff>=46&diff<=50	
		gen min51100a1=diff>=51&diff<=100
		gen min101a1=diff>=101 & diff<=144
		
		
		/****IS: Bins around old and new min wage****/
		/****JT: These have been recoded using old vs. current minimum wage ***/
		gen minold2new12=1 if diff<0 & diff2>0
		replace minold2new12=0 if minold2new12!=1
		
		gen minold2new312=1 if diff<0 & diff312>0
		replace minold2new312=0 if minold2new312!=1
		
		*Added - new definition of dynanmics
		gen minold12 = diff2==0
		gen minold312 = diff312==0
		
		gen min6b12=diff2<-5
		gen min35b12=diff2>=-5&diff2<=-3
		gen min12b12=diff2==-2|diff2==-1
		
		gen min6b312=diff312<-5
		gen min35b312=diff312>=-5&diff312<=-3
		gen min12b312=diff312==-312|diff312==-1

/*Forward minimum wage terms*/	



		gen minold2newf12= diff>0 & difff12<0

		gen minnewf12=difff12==0

		gen min12a2f=difff12>=1&difff12<=2
		gen min35a2f=difff12>=3&difff12<=5
		gen min610a2f=difff12>=6&difff12<=10
		gen min1115a2f=difff12>=11&difff12<=15
		gen min1620a2f=difff12>=16&difff12<=20
		gen min2125a2f=difff12>=21&difff12<=25
		gen min2630a2f=difff12>=26&difff12<=30
		gen min3135a2f=difff12>=31&difff12<=35
		gen min3640a2f=difff12>=36&difff12<=40
		gen min4145a2f=difff12>=41&difff12<=45
		gen min4650a2f=difff12>=46&difff12<=50 

		*gen smin6b=min6b*sub
		*gen smin35b=min35b*sub
		*gen smin12b=min12b*sub
		*gen smin=min*sub
		*gen smin13a=min13a*sub
		*gen smin410a=min410a*sub
		*gen smin1120a=min1120a*sub
		*gen smin2130a=min2130a*sub
		*gen smin3140a=min3140a*sub
		*gen smin4150a=min4150a*sub



		gen cut=wagcat

		/****TL: FIVE SEGMENTS INSTEAD OF TEN****/
			gen part0=.
			  replace part0=1 if cut<=63  /*up to $10*/
			  replace part0=2 if cut>=64&cut<=88 /*$10 to $12.5*/
			  replace part0=3 if cut>=89&cut<=113 /*$12.5 to $15.0*/
			  replace part0=4 if cut>=114&cut<=138 /*$15 to $17.5*/
			  replace part0=5 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=1 if cut<=33  /*up to $7*/
			*replace part0=2 if cut>=34&cut<=43 /*$7 to $8*/
			*replace part0=3 if cut>=44&cut<=53 /*$8 to $9*/
			*replace part0=4 if cut>=54&cut<=63 /*$9 to $10*/
			*replace part0=5 if cut>=64&cut<=73 /*$10 to $11*/
			*replace part0=6 if cut>=74&cut<=83 /*$11 to $12*/
			*replace part0=7 if cut>=84&cut<=98 /*$12 to $13.5*/
			*replace part0=8 if cut>=99&cut<=113 /*$13.5 to $15*/
			*replace part0=9 if cut>=114&cut<=138 /*$15 to $17.5*/
			*replace part0=10 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=11 if cut==164 /*$20 and above*/

		/*gen part=.
			replace part=1 if cut<=36
			replace part=2 if cut>=37&cut<=42
			replace part=3 if cut>=43&cut<=48
			replace part=4 if cut>=49&cut<=55
			replace part=5 if cut>=56&cut<=64
			replace part=6 if cut>=65&cut<=74
			replace part=7 if cut>=75&cut<=88
			replace part=8 if cut>=89&cut<=103
			replace part=9 if cut>=104&cut<=124
			replace part=10 if cut>=125&cut<=163
			replace part=11 if cut==164   */

			
			
			
		

		*removed sub...added mwchg
		*sort prov year month agegrp edgrp mincat mincatn mwchg
		*sort prov year month agegrp edgrp mincat mincatn mwchg wagcat
		bys prov year month female agegrp edgrp (wagcat): g rightsum=sum(fweight)
		bys prov year month female agegrp edgrp (wagcat): egen totalsum=max(rightsum)
		g remain=totalsum-rightsum+fweight
		drop rightsum totalsum
		

		
		// Original code which is too slow
		/*
		g remain2=0
		local i=1
		while `i'<=10 {
		egen rightsum=sum(fweight*(wagcat>=`i')),by(prov year quarter agegrp edgrp mincat mincatn mwchg)
		*gen hzd=fweight/rightsum
		replace remain2=rightsum if cut==`i'
		*replace h1=hzd if cut==`i'
		gen byte bin`i'=cut==`i'
		drop rightsum 
		local i=`i'+1
		}
		*/
		local i=1
		while `i'<=164 {
			gen byte bin`i'=cut==`i'
			local i=`i'+1
		}		

		compress
		
/* JT - Can this further be compressed on a quarterly basis? Months within a quarter can very in the following ways:
			1) location of the minimum wage
			2) location of the previous wage****
			3) wage dyanmics*/
			

		

		save $resultdir\data_joiners_`sex'_`k'_`j'_v1,replace


		local i=1
		while `i'<=5 {
		di "Partition" `i'
		*Variables used in regression
		gen byte p10_`i'=prov==10 & part0 ==`i' 
		gen byte p11_`i'=prov==11 & part0 ==`i' 
		gen byte p12_`i'=prov==12 & part0 ==`i' 
		gen byte p13_`i'=prov==13 & part0 ==`i' 
		gen byte p24_`i'=prov==24 & part0 ==`i' 
		gen byte p46_`i'=prov==46 & part0 ==`i' 
		gen byte p47_`i'=prov==47 & part0 ==`i' 
		gen byte p48_`i'=prov==48 & part0 ==`i' 
		gen byte p59_`i'=prov==59 & part0 ==`i' 

		gen byte a1_`i'=agegrp==1& part0==`i'
		gen byte a2_`i'=agegrp==2& part0==`i'
		gen byte a3_`i'=agegrp==3& part0==`i'
		gen byte a5_`i'=agegrp==5& part0==`i'

		gen byte e1_`i'=edgrp==1& part0==`i'
		gen byte e2_`i'=edgrp==2& part0==`i'
		gen byte e4_`i'=edgrp==4& part0==`i'
		
		gen byte f_`i'=female==1 & part0==`i'

		gen byte q1_`i'=quarter==1& part0==`i'
		gen byte q2_`i'=quarter==2& part0==`i'
		gen byte q4_`i'=quarter==4& part0==`i'

		*Commented out 1996 and 2012 for public use files.
		*gen byte y96_`i'=year==1996 & part0 ==`i' 
		gen byte y97_`i'=year==1997 & part0 ==`i' 
		gen byte y98_`i'=year==1998 & part0 ==`i' 
		gen byte y00_`i'=year==2000 & part0 ==`i'
		gen byte y01_`i'=year==2001 & part0 ==`i' 
		gen byte y02_`i'=year==2002 & part0 ==`i' 
		gen byte y03_`i'=year==2003 & part0 ==`i' 
		gen byte y04_`i'=year==2004 & part0 ==`i'
		gen byte y05_`i'=year==2005 & part0 ==`i' 
		gen byte y06_`i'=year==2006 & part0 ==`i' 
		gen byte y07_`i'=year==2007 & part0 ==`i' 
		gen byte y08_`i'=year==2008 & part0 ==`i'
		gen byte y09_`i'=year==2009 & part0 ==`i' 
		gen byte y10_`i'=year==2010 & part0 ==`i' 
		gen byte y11_`i'=year==2011 & part0 ==`i' 
		gen byte y12_`i'=year==2012 & part0 ==`i'
		gen byte y13_`i'=year==2013 & part0 ==`i'
		gen byte y14_`i'=year==2014 & part0 ==`i'
		gen byte y15_`i'=year==2015 & part0 ==`i'
		gen byte y16_`i'=year==2016 & part0 ==`i'
		
		di "provincial trends" `i'

		/*****TL: ADD PROVINCIAL TRENDS****/
		*gen byte ont_`i'=prov==35 & part0 ==`i' 
		gen byte tr10_`i'=(year-1999)*p10_`i' 
		gen byte tr11_`i'=(year-1999)*p11_`i' 
		gen byte tr12_`i'=(year-1999)*p12_`i' 
		gen byte tr13_`i'=(year-1999)*p13_`i' 
		gen byte tr24_`i'=(year-1999)*p24_`i' 
		*gen tr35_`i'=(year-1999)*ont_`i' 
		gen byte tr46_`i'=(year-1999)*p46_`i' 
		gen byte tr47_`i'=(year-1999)*p47_`i' 
		gen byte tr48_`i'=(year-1999)*p48_`i' 
		gen byte tr59_`i'=(year-1999)*p59_`i' 

		gen int tq10_`i'=(year-1999)^2*p10_`i' 
		gen int tq11_`i'=(year-1999)^2*p11_`i' 
		gen int tq12_`i'=(year-1999)^2*p12_`i' 
		gen int tq13_`i'=(year-1999)^2*p13_`i' 
		gen int tq24_`i'=(year-1999)^2*p24_`i' 
		*gen tq35_`i'=(year-1999)^2*ont_`i' 
		gen int tq46_`i'=(year-1999)^2*p46_`i' 
		gen int tq47_`i'=(year-1999)^2*p47_`i' 
		gen int tq48_`i'=(year-1999)^2*p48_`i' 
		gen int tq59_`i'=(year-1999)^2*p59_`i' 

		/******ADD PROV*BIN AND YEAR*BIN CONTROLS*****/
		gen byte bp10_`i'=(wagcat-(14+25*`i'))*p10_`i' 
		gen byte bp11_`i'=(wagcat-(14+25*`i'))*p11_`i' 
		gen byte bp12_`i'=(wagcat-(14+25*`i'))*p12_`i' 
		gen byte bp13_`i'=(wagcat-(14+25*`i'))*p13_`i' 
		gen byte bp24_`i'=(wagcat-(14+25*`i'))*p24_`i' 
		gen byte bp46_`i'=(wagcat-(14+25*`i'))*p46_`i' 
		gen byte bp47_`i'=(wagcat-(14+25*`i'))*p47_`i' 
		gen byte bp48_`i'=(wagcat-(14+25*`i'))*p48_`i' 
		gen byte bp59_`i'=(wagcat-(14+25*`i'))*p59_`i' 

		*gen by96_`i'=(wagcat-(14+25*`i'))*y96_`i'
		gen byte by97_`i'=(wagcat-(14+25*`i'))*y97_`i' 
		gen byte by98_`i'=(wagcat-(14+25*`i'))*y98_`i' 
		gen byte by00_`i'=(wagcat-(14+25*`i'))*y00_`i' 
		gen byte by01_`i'=(wagcat-(14+25*`i'))*y01_`i' 
		gen byte by02_`i'=(wagcat-(14+25*`i'))*y02_`i' 
		gen byte by03_`i'=(wagcat-(14+25*`i'))*y03_`i' 
		gen byte by04_`i'=(wagcat-(14+25*`i'))*y04_`i' 
		gen byte by05_`i'=(wagcat-(14+25*`i'))*y05_`i' 
		gen byte by06_`i'=(wagcat-(14+25*`i'))*y06_`i' 
		gen byte by07_`i'=(wagcat-(14+25*`i'))*y07_`i' 
		gen byte by08_`i'=(wagcat-(14+25*`i'))*y08_`i' 
		gen byte by09_`i'=(wagcat-(14+25*`i'))*y09_`i' 
		gen byte by10_`i'=(wagcat-(14+25*`i'))*y10_`i' 
		gen byte by11_`i'=(wagcat-(14+25*`i'))*y11_`i' 
		gen byte by12_`i'=(wagcat-(14+25*`i'))*y12_`i' 
		gen byte by13_`i'=(wagcat-(14+25*`i'))*y13_`i' 
		gen byte by14_`i'=(wagcat-(14+25*`i'))*y14_`i' 
		gen byte by15_`i'=(wagcat-(14+25*`i'))*y15_`i' 
		gen byte by16_`i'=(wagcat-(14+25*`i'))*y16_`i' 

		di "End of loop" `i'

		if `i'==5 {
			sum tr* tq* bp* by*_1
		}
	
		local i=`i'+1
		} /*end i-loop */

		/*****TL: ADD A FULL SET OF prov*year DUMMIES (SEGMENT 1)***/
		gen provyear=100*prov+(year-1997) if part0==1
		replace provyear=9999 if part0>=2
		quietly tab provyear, gen(py)

		/*NOTE: ONTARIO IN 1999 IS py103*/
		/*Note: without 1996, py200 is the last valid dummy (py201 is for obs in the second and higher partition */

		gen cmin6b12=mwchg12*min6b12
		gen cmin35b12=mwchg12*min35b12
		gen cmin12b12=mwchg12*min12b12
		*gen cmin12=mwchg12*min1     JT - now use the current min. wage and work back.
		gen cminold12=mwchg12*minold12
		gen cminold2new12=mwchg12*minold2new12
		gen cmin12=mwchg12*min1
		gen cmin12a12=mwchg12*min12a1
		gen cmin35a12=mwchg12*min35a1
		gen cmin610a12=mwchg12*min610a1
		gen cmin1115a12=mwchg12*min1115a1
		gen cmin1620a12=mwchg12*min1620a1
		gen cmin2125a12=mwchg12*min2125a1
		gen cmin2630a12=mwchg12*min2630a1
		gen cmin3135a12=mwchg12*min3135a1
		gen cmin3640a12=mwchg12*min3640a1
		gen cmin4145a12=mwchg12*min4145a1
		gen cmin4650a12=mwchg12*min4650a1
		
		gen cmin6b312=mwchg312*min6b312
		gen cmin35b312=mwchg312*min35b312
		gen cmin12b312=mwchg312*min12b312
		*gen cmin312=mwchg312*min1
		gen cminold312=mwchg312*minold312
		gen cminold2new312=mwchg312*minold2new312
		gen cmin312=mwchg312*min1	
		gen cmin12a312=mwchg312*min12a1
		gen cmin35a312=mwchg312*min35a1
		gen cmin610a312=mwchg312*min610a1
		gen cmin1115a312=mwchg312*min1115a1
		gen cmin1620a312=mwchg312*min1620a1
		gen cmin2125a312=mwchg312*min2125a1
		gen cmin2630a312=mwchg312*min2630a1
		gen cmin3135a312=mwchg312*min3135a1
		gen cmin3640a312=mwchg312*min3640a1
		gen cmin4145a312=mwchg312*min4145a1
		gen cmin4650a312=mwchg312*min4650a1
		
/*********** Forward dynamic terms - Added 9/9/24 by James. ***/

		gen cmin6bf12=mwchgf12*min6b1
		gen cmin35bf12=mwchgf12*min35b1
		gen cmin12bf12=mwchgf12*min12b1
		gen cminf12=mwchgf12*min1
		gen cminnewf12=mwchgf12*minnewf12
		*gen cminoldf12=mwchgf12*minoldf12
		gen cminold2newf12=mwchgf12*minold2newf12
		gen cmin12af12=mwchgf12*min12a2f
		gen cmin35af12=mwchgf12*min35a2f
		gen cmin610af12=mwchgf12*min610a2f
		gen cmin1115af12=mwchgf12*min1115a2f
		gen cmin1620af12=mwchgf12*min1620a2f
		gen cmin2125af12=mwchgf12*min2125a2f
		gen cmin2630af12=mwchgf12*min2630a2f
		gen cmin3135af12=mwchgf12*min3135a2f
		gen cmin3640af12=mwchgf12*min3640a2f
		gen cmin4145af12=mwchgf12*min4145a2f
		gen cmin4650af12=mwchgf12*min4650a2f
		
		gen dolm=dollar*min1

				
		
/* Used to check above definitions

sum cmin*12 if prov==10 & month==4 & year==1997
sum cmin*12 if prov==10 & month==6 & year==1997
table wagcat if prov==10 & month==6 & year==1997, stat(mean cmin6b312) stat(mean /// 
cminold312) stat(mean cminold2new312) stat(mean cminnew312)

*/		

/* code checks definitions 
For Nfld, minimum wage increase from 7 to 7.5 in October 2007, followed by minimum wage increases from 7.5 to 8 in April 2008*/
/* codes indicating past/upcominng minimum wage changes */
/*table wagcat month if year==2008 & prov==10 & wagcat>28 & wagcat<=35, stat(mean mwchg) stat(mean mwchg12) stat(mean mwchg312) stat(mean mwchgf12) stat(mean untilchange) stat(mean mwage) stat(mean sincechange)

table wagcat month if year==2008 & prov==10 & wagcat<=35, stat(mean min1) stat(mean cmin12) stat(mean cmin312) stat(mean cminnewf12) stat(mean rminw) stat(mean rminwf12) stat(mean cpi)
table wagcat month if year==2008 & prov==10 & wagcat<=38, stat(mean cmin6bf12) stat(mean cmin35bf12) stat(mean cmin12bf12) stat(mean cminf12) stat(mean cminold2newf12) stat(mean cminnewf12) stat(mean cmin12af12) */



		/*glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a cmin cmin6b cmin35b cmin12b cmin12a ///
		cmin35a cmin610a cmin1115a cmin1620a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)


		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a  ///
		dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\aug24nd_`sex'_`k'_`j',replace

		 glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a min2125a min2630a ///
		 dollar dolm ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\dec15nda_`sex'_`k'_`j',replace 

		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a  ///
		cmin cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\aug24d_`sex'_`k'_`j',replace

		glm fweight min min6b min35b min12b min12a min35a min610a ///
		min1115a min1620a min2125a min2630a ///
		cmin cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a cmin2125a cmin2630a dollar dolm ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\dec15da_`sex'_`k'_`j',replace */

egen est_nobs=sum(one)
sum est_nobs



		


 
if `sex'<=2 {
	
	
		
/*		*  - Full specification with extended minimum wage
 
		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1 min51100a1 min101a1 ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\joiners_`sex'_`k'_`j'_extend,replace		
*/	
	
	*  - Full specification with new 1-2 month lag definitions.
 
		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6bf12 cmin35bf12 cmin12bf12 cminf12 cminold2newf12 ///
		cminnewf12 cmin12af12 cmin35af12 cmin610af12 cmin1115af12 ///
		cmin1620af12 cmin2125af12 cmin2630af12 cmin3135af12 ///
		cmin3640af12 cmin4145af12 cmin4650af12 dollar ///
		a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\leavers_`sex'_`k'_`j'_layoff_original_forward,replace	

/*	*  - Full specification with new 1-2 month lag definitions, no constant.
 
		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 dollar ///
		a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov) noconstant

		est save $resultdir\joiners_`sex'_`k'_`j'_original_nc,replace	
*/		
/*----		*Full specification with minimum wage effects up to 12 months after a change.

		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 ///
		cmin6b312 cmin35b312 cmin12b312 cminold312 cminold2new312 cmin312  ///
		cmin12a312 cmin35a312 ///
		cmin610a312 cmin1115a312 cmin1620a312 cmin2125a312 cmin2630a312 ///
		cmin3135a312 cmin3640a312 cmin4145a312 cmin4650a312 ///
		dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\joiners_`sex'_`k'_`j'_1_lag,replace		
-----*/		
		
		*Full specification with minimum wage effects up to 12 months after a change AND
		*FORWARD looking terms.

		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 ///
		cmin6b312 cmin35b312 cmin12b312 cminold312 cminold2new312 cmin312  ///
		cmin12a312 cmin35a312 ///
		cmin610a312 cmin1115a312 cmin1620a312 cmin2125a312 cmin2630a312 ///
		cmin3135a312 cmin3640a312 cmin4145a312 cmin4650a312 ///
		cmin6bf12 cmin35bf12 cmin12bf12 cminf12 cminold2newf12 ///
		cminnewf12 cmin12af12 cmin35af12 cmin610af12 ///
		cmin1115af12 cmin1620af12 cmin2125af12 cmin2630af12 ///
		cmin3135af12 cmin3640af12 cmin4145af12 cmin4650af12 ///
		dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\leavers_`sex'_`k'_`j'_layoff_1_lag_forward,replace
		

		
}

/* if `sex'<=2	{

		drop if part0==4|part0==5
		
		*Full specification with minimum wage effects up to 12 months after a change.

		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 ///
		cmin6b312 cmin35b312 cmin12b312 cminold312 cminold2new312 cmin312  ///
		cmin12a312 cmin35a312 ///
		cmin610a312 cmin1115a312 cmin1620a312 cmin2125a312 cmin2630a312 ///
		cmin3135a312 cmin3640a312 cmin4145a312 cmin4650a312 ///
		dollar ///
		a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		bin1-bin113 if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov) noconstant

		est save $resultdir\joiners_`sex'_`k'_`j'_1_lag_trunc,replace		
		
}
*/

 
if `sex'==3 {
	
	*  - Full specification with new 1-2 month lead definitions.
 
		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6bf12 cmin35bf12 cmin12bf12 cminf12 cminold2newf12 ///
		cminnewf12 cmin12af12 cmin35af12 cmin610af12 cmin1115af12 ///
		cmin1620af12 cmin2125af12 cmin2630af12 cmin3135af12 ///
		cmin3640af12 cmin4145af12 cmin4650af12 dollar ///
		f_1 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		f_2 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		f_3 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		f_4 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		f_5 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\leavers_`sex'_`k'_`j'_layoff_original_forward,replace	

/*	*  - Full specification with new 1-2 month lag definitions, no constant.
 
		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6bf12 cmin35bf12 cmin12bf12 cminf12 cminold2newf12 ///
		cminnewf12 cmin12af12 cmin35af12 cmin610af12 cmin115af12 ///
		cmim1620af12 cmin2125af12 cmin2630af12 cmin3135af12 ///
		cmin3640af12 cmin4145af12 cmin4650af12 ///
		dollar ///
		a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov) noconstant

		est save $resultdir\joiners_`sex'_`k'_`j'_original_nc,replace	
*/		
		*Full specification with minimum wage effects up to 12 months after a change 
		*and 1-2 month lead.

		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 ///
		min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1  ///
		cmin6b12 cmin35b12 cmin12b12 cminold12 cminold2new12 cmin12  ///
		cmin12a12 cmin35a12 ///
		cmin610a12 cmin1115a12 cmin1620a12 cmin2125a12 cmin2630a12 ///
		cmin3135a12 cmin3640a12 cmin4145a12 cmin4650a12 ///
		cmin6b312 cmin35b312 cmin12b312 cminold312 cminold2new312 cmin312  ///
		cmin12a312 cmin35a312 ///
		cmin610a312 cmin1115a312 cmin1620a312 cmin2125a312 cmin2630a312 ///
		cmin3135a312 cmin3640a312 cmin4145a312 cmin4650a312 ///
		cmin6bf12 cmin35bf12 cmin12bf12 cminf12 cminold2newf12 ///
		cminnewf12 cmin12af12 cmin35af12 cmin610af12 cmin1115af12 ///
		cmin1620af12 cmin2125af12 cmin2630af12 cmin3135af12 ///
		cmin3640af12 cmin4145af12 cmin4650af12 dollar ///
		f_1 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		f_2 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		f_3 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		f_4 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		f_5 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\leavers_`sex'_`k'_`j'_layoff_1_lag_forward,replace		
		
}

			
		
			
erase $resultdir\data_joiners_`sex'_`k'_`j'_v1.dta
} /*End j-loop  */
	 

	*local k=`k'+1
	*}

	local sex=`sex'+1
} /*end sex loop*/


log close




